home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / pnt_line.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  85 lines

  1. ; $Id: pnt_line.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1993-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. Function Pnt_Line, p0, l0, l1, pl, INTERVAL = interval
  7. ;+
  8. ; NAME:
  9. ;    PNT_LINE
  10. ;
  11. ; PURPOSE:
  12. ;    This function returns the perpendicular distance between the
  13. ;    point P0 and the line between points L0 and L1.
  14. ;
  15. ; CATEGORY:
  16. ;    Geometry.
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;    Result = PNT_LINE(P0, L0, L1 [, Pl])
  20. ;
  21. ; INPUTS:
  22. ;    P0: The location of the point. P0 may have 2 to N elements,
  23. ;            for N dimensions.
  24. ;    L0: One end-point of the line. L0 must have same number of
  25. ;        elements as P0.
  26. ;    L1: The other end-point of the line. L1 must have the same
  27. ;        number of elements as LO.
  28. ;
  29. ; KEYWORD PARAMETERS:
  30. ;    INTERVAL: If set, and if the point on the line between L0
  31. ;          and L1 that is closest to PO is not within the
  32. ;          interval [L0, L1], causes the function to return
  33. ;          the distance from P0 to the closer of the two
  34. ;          endpoints L0 and L1.
  35. ;
  36. ; OUTPUTS:
  37. ;    This function returns the distance from point P0 to the line
  38. ;    between L0 and L1, unless the closest point on the line is
  39. ;    not in the interval [L0, L1] and the keyword INTERVAL is set.
  40. ;    In this case, the function returns the distance between P0
  41. ;    and the closer of the two end-points.
  42. ;
  43. ; OPTIONAL OUTPUTS:
  44. ;    Pl: The point on the line between L0 and L1 that is closest to P0.
  45. ;        Pl is not necessarily in the interval [L0, L1].
  46. ;
  47. ; RESTRICTIONS:
  48. ;    This function is limited by the machine accuracy of single
  49. ;    precision floating point.
  50. ;
  51. ; PROCEDURE:
  52. ;    Solve equations of perpendicular, etc.
  53. ;
  54. ; EXAMPLE:
  55. ;    To print the distance between the point (2,3) and the line
  56. ;    from (-3,3) to (5,12), and also the location of the point on
  57. ;    the line closest to (2,3), enter the following command:
  58. ;
  59. ;      PRINT, PNT_LINE([2,3], [-3,3], [5,12], Pl), Pl
  60. ;
  61. ; MODIFICATION HISTORY:
  62. ;     DMS, RSI, Jan, 1993.    Written.
  63. ;-
  64.  
  65.  
  66. lv = float(l1 - l0)
  67. l = sqrt(total(lv*lv))
  68. if l eq 0 then begin    ;Line is a point
  69.     pl = p0
  70.     return, sqrt(total((l0-float(p0))^2))
  71.     endif
  72. lv = lv / l            ;Normal to line
  73. t = - (-total(lv * p0) + total(l0 * lv))/(total(lv * lv))
  74. pl = t * lv + l0        ;Closest point on line
  75. out = (t lt 0) or (t gt l)    ;Within interval?
  76.  
  77. if keyword_set(interval) and out then begin  ;Outside interval?
  78.     d1 = sqrt(total((p0-l1)^2))
  79.     d2 = sqrt(total((p0-l0)^2))
  80.     return, d1 < d2
  81. endif else return, sqrt(total((p0 - pl)^2))
  82.  
  83. end
  84.  
  85.